#ifndef _flash_memory
#define _flash_memory

#define FLASH_COMMAND_READ  0x03
#define FLASH_COMMAND_SECTOR_ERASE 0x20
#define FLASH_COMMAND_BLOCK_ERASE 0x52
#define FLASH_COMMAND_CHIP_ERASE 0x60
#define FLASH_COMMAND_BYTE_PROGRAM 0x02
#define FLASH_COMMAND_AAI_PROGRAM 0xAF
#define FLASH_COMMAND_READ_STATUS 0x05
#define FLASH_COMMAND_ENABLE_WRITE_STATUS 0x50
#define FLASH_COMMAND_WRITE_STATUS 0x01
#define FLASH_COMMAND_WRITE_ENABLE 0x06
#define FLASH_COMMAND_WRITE_DISABLE 0x04
#define FLASH_COMMAND_READ_ID 

// The memory addresses are 23 bits long, but are transmitted in 3 bytes via SPI
// The first bit of high is ignored
typedef struct 
{
	unsigned char high;
	unsigned char medium;
	unsigned char low;
} Flash_Address;

// Read a single byte from the flash memory
void flash_read_bytes(Flash_Address addr, unsigned char * bytes, unsigned char numBytes);
// Enable writing to the flash memory
void flash_write_enable(void);
// Disable writing to the flash memory
void flash_write_disable(void);
// Write bytes to the memory in AAI mode
void flash_write_bytes(Flash_Address addr, unsigned char * bytes, unsigned char numBytes);

// Erase a 4 Kbyte sector using bits 23-12 of the address
void flash_erase_sector(Flash_Address addr);

// Get the status register of the flash chip
unsigned char flash_get_status(void);

// Handle successive aai writes
void flash_aai_interrupt_handler(void);

// Handle successive byte reads
void spi_read_bytes(void);

// Signal memory chip to accept data
void spi_flash_memory_enable(void);
// Signal memory chip to stop accepting data
void spi_flash_memory_disable(void);

#endif